package com.dut.bbs.activity;

import org.apache.http.impl.client.DefaultHttpClient;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.dut.bbs.R;
import com.dut.bbs.entity.LoginEntity;
import com.dut.bbs.util.Dg;
import com.dut.bbs.web.HttpClientFactory;

public class MainActivity extends TabActivity {
	class TAB_TAG {
		static final String TOP10 = "top10"; // 全站十大
		static final String CATEGORY = "category"; // 版面
		static final String EMAIL = "email"; // 邮箱
		static final String FAV = "fav"; // 热门话题
	}
	public void onCreate(Bundle savedInstanceState) {
	     super.onCreate(savedInstanceState);
	     setTitle("十大热门");
	     setContentView(R.layout.tab);
	     TabHost tabHost = getTabHost();
	     Resources res = getResources();
	     tabHost.addTab(tabHost.newTabSpec(TAB_TAG.TOP10)
	    		 .setIndicator("十大", res.getDrawable(R.drawable.top))
	    		 .setContent(new Intent(this,Top10Activity.class)
	    		 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
	     tabHost.addTab(tabHost.newTabSpec(TAB_TAG.CATEGORY)
	    		 .setIndicator("版面", res.getDrawable(R.drawable.board))
	    		 .setContent(new Intent(this, CategoryActivity.class)
	    		 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
	     tabHost.addTab(tabHost.newTabSpec(TAB_TAG.EMAIL)
	    		 .setIndicator("邮箱", res.getDrawable(R.drawable.mailbox))
	    		 .setContent(new Intent(this, MailActivity.class)
	    		 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
	     tabHost.addTab(tabHost.newTabSpec(TAB_TAG.FAV)
	    		 .setIndicator("热门话题", res.getDrawable(R.drawable.hot))
	    		 .setContent(new Intent(this, FavoriteActivity.class)
	    		 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
	     tabHost.setCurrentTab(0); 
	     tabHost.bringToFront();
	     tabHost.setOnTabChangedListener(new OnTabChangeListener(){

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if(TAB_TAG.TOP10.equals(tabId))
					MainActivity.this.setTitle("十大热门");
				else if(TAB_TAG.EMAIL.endsWith(tabId))
					MainActivity.this.setTitle("我的信箱");
				else if(TAB_TAG.CATEGORY.equals(tabId))
					MainActivity.this.setTitle("选择版面");
				else
					MainActivity.this.setTitle("热门推荐");
			}
	    	 
	     });
	     getTabWidget().getChildAt(2).setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!LoginEntity.isLogin())
					Dg.showToast(MainActivity.this, "你还没有登录呢");
				else
					getTabHost().setCurrentTab(2);
			}
	    	 
	     });
	}
	
	@Override 
    public boolean dispatchKeyEvent(KeyEvent event) { 
        if (event.getAction() == KeyEvent.ACTION_DOWN 
                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { 
        	dialog_Exit(this);
            return false; 
        } 
        return false; 
    }; 
    
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	menu.add(0, Menu.FIRST, 0, "设置").setIcon(R.drawable.menu_setting);
    	menu.add(0,Menu.FIRST+1,2,"注销").setIcon(R.drawable.menu_exit);
    	menu.add(0,Menu.FIRST+2,1,"反馈").setIcon(R.drawable.menu_sendmail);
    	return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    	case Menu.FIRST:
    		Intent in = new Intent(MainActivity.this,SettingActivity.class);
    		startActivity(in);
    		break;
    	case Menu.FIRST+1:
    		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    		SharedPreferences.Editor editor = settings.edit();
    			editor.remove("name");
    			editor.remove("password");
    			editor.remove("autologin");
    			editor.commit();
//    			LoginEntity.logout();
    			DefaultHttpClient client= (DefaultHttpClient)HttpClientFactory.getHttpClient();
    			client.getCookieStore().clear();
    		Intent in2 = new Intent(MainActivity.this,LoginActivity.class);
				startActivity(in2);
				this.finish();
				break;
    	case Menu.FIRST+2:
    		Intent data=new Intent(Intent.ACTION_SENDTO);
    		data.setData(Uri.parse("mailto:bhqt_android@126.com"));
    		startActivity(data);
    		break;
    	}
    	return true;
    }
    public void dialog_Exit(Context context) {

		AlertDialog.Builder builder = new Builder(context);
		builder.setMessage("确定要退出吗?");
		builder.setTitle("提示");
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setPositiveButton("确认",
		new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				MainActivity.this.finish();
				android.os.Process.killProcess(android.os.Process.myPid()); 
			}

		});

		builder.setNegativeButton("取消",

		new android.content.DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builder.create().show();

	}
 

}
